package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayNewsCategory;
import org.itboys.mobile.service.mongo.web.SundayNewsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/23
 * 描述：雄安加盟网_资讯
 */
@Controller
@RequestMapping("/sunday/web/news/category")
public class SundayNewsCategoryController extends BaseController{
    @Autowired
    private SundayNewsCategoryService categoryService;
    /**
     * 资讯系列主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model){
        return "/sunday/news/category/index";
    }

    /**
     * 获取树形资讯分类
     * @param request
     * @param response
     */
    @RequestMapping("/getTree")
    public void getTree(HttpServletRequest request,
                        HttpServletResponse response
    ) {
        try {

            List<SundayNewsCategory> tree = categoryService.getCategoryTreeJson();
            AjaxUtils.renderJson(response, tree);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 单个查询
     * @param request
     * @param response
     */
    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="id",required=true) Long id

    ) {
        try {

            SundayNewsCategory category = categoryService.getById(id);
            AjaxUtils.renderJson(response, category);
        } catch (Exception e) {
            e.printStackTrace();

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/news/category/findOne------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或修改产品分类
     * @param request
     * @param response
     * @param category
     * @param imageFile
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayNewsCategory category,
                     @RequestParam(value="imageFile",required=false) MultipartFile imageFile
    ) {
        try {
            categoryService.save(category, imageFile);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            e.printStackTrace();
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/news/category/save------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }


    /**
     * 获取所有分类
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("isDeleted",0);
            List<SundayNewsCategory> tree = categoryService.list(param);
            AjaxUtils.renderJson(response, tree);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 删除产品分类
     * @param request
     * @param response
     * @param categoryId
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="categoryId",required=true) Long categoryId

    ) {
        try {

            categoryService.deleteCategory(categoryId);

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/news/category/delete------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 资讯分类选择器
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="param",required=false)String param,//查询参数
                          @RequestParam(value="callBack",required=false)String callBack,//制定的回掉函数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/news/category/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/news/category/choose/choose_single";
        }else if(isSingle==0){
            return "/sunday/news/category/choose/choose_muilt";
        }

        return "/sunday/news/category/choose/choose_erro";
    }

    /**
     * 设置
     * @param request
     * @param response
     * @param id
     * @param value
     * @param type
     */
    @RequestMapping("/set")
    public void set(HttpServletRequest request,
                    HttpServletResponse response,
                    @RequestParam(value="id",required=true) Long id,
                    @RequestParam(value="value",required=true) Integer value,
                    @RequestParam(value="type",required=true) Integer type
    ) {
        try {
            categoryService.set(id,type,value);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/category/set------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }
    }

}

